package
{
	import com.lib.playbook.controls.CheckBox;
	import com.lib.playbook.dialog.DialogBase;
	import com.lib.playbook.events.DialogEvent;
	
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import qnx.dialog.*;
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.core.UIComponent;
	import qnx.ui.text.Label;
	import qnx.ui.text.TextInput;
		
		public class EditServerDialog extends DialogBase
		{
			private var _name:TextInput;
			private var _url:TextInput;
			private var _port:TextInput;
			private var _pass:TextInput;
			private var _del:CheckBox;
			private var _index:int;
			private var _message_html    : String = '';
			private var _message         : String = '';
			private var _left_offset     : int = 0;
			public function EditServerDialog()
			{
				super();
				this.title = "Edit server";
				this.dialogSize = DialogSize.SIZE_TALL;
				
				_name = new TextInput();
				addChild(_name);
				_url = new TextInput();
				addChild(_url);
				_port = new TextInput();
				addChild(_port);
				_pass = new TextInput();
				addChild(_pass);
				_del = new CheckBox();
				_del.label = "Delete";
				addChild(_del);
				
				var myFormat:TextFormat = new TextFormat();
				myFormat.color = 0x000000;
				myFormat.size = 20;	//
				myFormat.bold = true;	// Changed to match Alert Dialog box
				myFormat.font = "Verdana";	//
				myFormat.align = "center";

				this.addButton("Save");
				this.addButton("Cancel");
				
				this.addEventListener(DialogEvent.DIALOG_BUTTON_CLICKED, validateForm);
			}
			override protected function draw():void
			{
				var textmsg :Label = new Label();
				textmsg.setSize( this.body.width - this._left_offset, this.body.height );
				textmsg.textField.wordWrap = true;
				textmsg.textField.multiline = true;
				
				var msgformat : TextFormat = new TextFormat();
				msgformat.align = TextFormatAlign.CENTER;
				
				textmsg.format = msgformat;
				textmsg.autoSize = TextFieldAutoSize.CENTER;
				
				if( this._message != '' )textmsg.text = this._message;
				else if( this._message_html != '' )textmsg.htmlText = this._message_html;
				
				
				//
				textmsg.setPosition( this.body.x + this._left_offset, this.body.y );
				this.addChild( textmsg );
				
				// name
				var nameLbl:Label = new Label();
				nameLbl.text = "Name";
				nameLbl.width = nameLbl.textWidth + 5;
				addChild(nameLbl);

				// url
				var urlLbl:Label = new Label();
				urlLbl.text = "Address";
				urlLbl.width = urlLbl.textWidth + 5;
				addChild(urlLbl);

				// port
				var portLbl:Label = new Label();
				portLbl.text = "Port";
				portLbl.width = portLbl.textWidth + 5;
				addChild(portLbl);
	
				// passworld
				var passLbl:Label = new Label();
				passLbl.text = "Password";
				passLbl.width = passLbl.textWidth + 5;
				addChild(passLbl);
				super.draw();
				
				var max_label_width : int = Math.max( urlLbl.width, nameLbl.width, portLbl.width, passLbl.width ) + 5;

				var yoffset : int = this.body.y + this.body.height;
				
				yoffset -= this._del.height;
				this._del.width = this.inner_frame.width;
				this._del.setPosition( this.inner_frame.x + max_label_width, yoffset );
				
				yoffset = yoffset - this.buttonGap - this._pass.height;
				this.setInputFieldPos( yoffset, max_label_width, _pass, passLbl);
				
				yoffset = yoffset - this.buttonGap - this._port.height;
				this.setInputFieldPos( yoffset, max_label_width, _port, portLbl);
				
				yoffset = yoffset - this.buttonGap - this._url.height;
				this.setInputFieldPos( yoffset, max_label_width, _url, urlLbl);
				
				yoffset = yoffset - this.buttonGap - this._name.height;
				this.setInputFieldPos( yoffset, max_label_width, _name, nameLbl);
			}
			
			private function setInputFieldPos(yoffset:int, divider:int, inputField:TextInput, lbl:Label):void
			{
				inputField.width = this.inner_frame.width - divider;
				inputField.setPosition( this.inner_frame.x + divider, yoffset );
				lbl.setPosition( this.inner_frame.x, inputField.y + 5 );
			}
			
			public function set networkName( value:String ):void
			{
				this._name.text = value;
			}
			public function get networkName():String
			{
				return this._name.text;
			}
			
			public function set address( value:String ):void
			{
				this._url.text = value;
			}
			public function get address():String
			{
				return this._url.text;
			}
			
			public function set port( value:String ):void
			{
				this._port.text = value;
			}
			public function get port():String
			{
				return this._port.text;
			}
			public function set password( value:String ):void
			{
				this._pass.text = value;
			}
			public function get password():String
			{
				return this._pass.text;
			}
			public function set index( value:int ):void
			{
				this._index = value;
			}
			public function get index():int
			{
				return this._index;
			}
			public function reset():void
			{
				this._name.text = '';
				this._url.text = '';
				this._pass.text = '';
				this._port.text = '';
				this._del.selected = false;
			}
			
			private function validateForm(evt:DialogEvent):void
			{
				if(evt.responseBtnText == "Cancel"){
					this.cancel();
					return;
				}
				if(this._del.selected){
					this.dispatchEvent(CreateEditServerDialogNotificationEvent());
					this.cancel();
					return;
				}
				if(this._name.text.length == 0 ||
				   this._port.text.length == 0 ||
				   this._url.text.length == 0){
					this._message_html = "<font color=red>Please complete fields..<font>";
					this.draw();
				}else{
					this.dispatchEvent(CreateEditServerDialogNotificationEvent());
					this.cancel();
				}
			}
			
			override protected function CreateNotificationEvent() :DialogEvent
			{
				var evt : DialogEvent = new DialogEvent( DialogEvent.DIALOG_BUTTON_CLICKED );
				return evt;
			}
			
			protected function CreateEditServerDialogNotificationEvent() :DialogEvent
			{
				var evt : EditServerDialogEvent = new EditServerDialogEvent( EditServerDialogEvent.DIALOG_CLOSED );
				evt.networkName   = this._name.text;
				evt.address 	  = this._url.text;
				evt.port 		  = this._port.text;
				evt.password      = this._pass.text;
				evt.del 		  = this._del.selected;
				evt.index		  = this._index;
				return evt;
			}
		}
}